package thesis.filesharing.services.fulltextsearch.indexing;

import java.util.List;

import thesis.filesharing.common.util.Utils;
import thesis.filesharing.model.Clazz;
import thesis.filesharing.model.ExerciseCategory;
import thesis.filesharing.model.Lecturer;
import thesis.filesharing.model.Student;
import thesis.filesharing.model.Subject;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.search.Document;
import com.google.appengine.api.search.Field;
import com.google.appengine.api.search.Index;
import com.google.appengine.api.search.IndexSpec;
import com.google.appengine.api.search.SearchServiceFactory;

public class DocumentIndexing {
	
	public static void addEntityIndex(Object obj, String indexName) {
		Document document = buildDocument(obj);
		getIndex(indexName).add(document);
	}
	
	public static void removeEntityIndex(String entityKey, String indexName) {
		getIndex(indexName).remove(entityKey);
	}
	
	public static void removeEntityIndexes(List<String> entityKeys, String indexName) {
		getIndex(indexName).remove(entityKeys);
	}
	
	private static Document buildDocument(Object obj) {
		Document document = null;
		if (obj instanceof Lecturer) {
			Lecturer lecturer = (Lecturer) obj;
			document = Document.newBuilder().setId(KeyFactory.keyToString(lecturer.getLecturerKey()))
					.addField(Field.newBuilder().setName("fLecturerId").setText(Utils.insertSignChar(Utils.filterVietnameseSign(lecturer.getLecturerId()))))
					.addField(Field.newBuilder().setName("fLecturerName").setText(Utils.insertSignChar(Utils.filterVietnameseSign(lecturer.getLecturerName()))))
					.addField(Field.newBuilder().setName("fLecturerEmail").setText(Utils.insertSignChar(Utils.filterVietnameseSign(lecturer.getLecturerEmail()))))
					.addField(Field.newBuilder().setName("fLecturerDepartment").setText(Utils.insertSignChar(Utils.filterVietnameseSign(lecturer.getLecturerDepartment()))))
					.addField(Field.newBuilder().setName("lecturerId").setText(lecturer.getLecturerId()))
					.addField(Field.newBuilder().setName("lecturerName").setText(lecturer.getLecturerName()))
					.addField(Field.newBuilder().setName("lecturerEmail").setText(lecturer.getLecturerEmail()))
					.addField(Field.newBuilder().setName("lecturerDepartment").setText(lecturer.getLecturerDepartment()))
					.build();
		} else if (obj instanceof Student) {
			Student student = (Student) obj;
			document = Document.newBuilder().setId(KeyFactory.keyToString(student.getStudentKey()))
					.addField(Field.newBuilder().setName("fStudentId").setText(Utils.insertSignChar(Utils.filterVietnameseSign(student.getStudentId()))))
					.addField(Field.newBuilder().setName("fStudentName").setText(Utils.insertSignChar(Utils.filterVietnameseSign(student.getStudentName()))))
					.addField(Field.newBuilder().setName("fStudentEmail").setText(Utils.insertSignChar(Utils.filterVietnameseSign(student.getStudentEmail()))))
					.addField(Field.newBuilder().setName("studentId").setText(student.getStudentId()))
					.addField(Field.newBuilder().setName("studentName").setText(student.getStudentName()))
					.addField(Field.newBuilder().setName("studentEmail").setText(student.getStudentEmail()))
					.addField(Field.newBuilder().setName("className").setText(student.getClassName()))
					.addField(Field.newBuilder().setName("course").setText(String.valueOf(student.getCourse())))
					.build();
		} else if (obj instanceof Subject) {
			Subject subject = (Subject) obj;
			document = Document.newBuilder().setId(KeyFactory.keyToString(subject.getSubjectKey()))
					.addField(Field.newBuilder().setName("fSubjectId").setText(Utils.insertSignChar(Utils.filterVietnameseSign(subject.getSubjectId()))))
					.addField(Field.newBuilder().setName("fSubjectName").setText(Utils.insertSignChar(Utils.filterVietnameseSign(subject.getSubjectName()))))
					.addField(Field.newBuilder().setName("subjectId").setText(subject.getSubjectId()))
					.addField(Field.newBuilder().setName("subjectName").setText(subject.getSubjectName()))
					.build();
		} else if (obj instanceof Clazz) {
			Clazz clazz = (Clazz) obj;
			document = Document.newBuilder().setId(KeyFactory.keyToString(clazz.getClassKey()))
					.addField(Field.newBuilder().setName("fClassId").setText(Utils.insertSignChar(Utils.filterVietnameseSign(clazz.getClassId()))))
					.addField(Field.newBuilder().setName("fClassName").setText(Utils.insertSignChar(Utils.filterVietnameseSign(clazz.getClassName()))))
					.addField(Field.newBuilder().setName("classId").setText(clazz.getClassId()))
					.addField(Field.newBuilder().setName("className").setText(clazz.getClassName()))
					.addField(Field.newBuilder().setName("semester").setNumber(clazz.getSemester()))
					.build();
		} else if (obj instanceof ExerciseCategory) {
			ExerciseCategory tag = (ExerciseCategory) obj;
			document = Document.newBuilder().setId(KeyFactory.keyToString(tag.getCategoryKey()))
					.addField(Field.newBuilder().setName("fCategoryName").setText(Utils.insertSignChar(Utils.filterVietnameseSign(tag.getCategoryName()))))
					.addField(Field.newBuilder().setName("categoryName").setText(tag.getCategoryName()))
					.addField(Field.newBuilder().setName("ranking").setText(String.valueOf(tag.getRanking())))
					.build();
		}
		return document;
	}
	
	public static Index getIndex(String indexName) {
		IndexSpec spec = IndexSpec.newBuilder().setName(indexName).build();
		return SearchServiceFactory.getSearchService().getIndex(spec);
	}
}
